<?php
require_once '../DTO/Tinh.php';
require_once 'DBConnection.php';

class TinhDAO extends DBConnection{
	public function __construct(){
		parent::__construct();
	}
	
	//Lấy danh sách tất cả các tỉnh
	public function getTinh(){
		$sql = "select * from Tinh";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new Tinh($temp['MaTinh'], $temp['TenTinh'], $temp['MaVung']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy danh sách các tỉnh theo mã vùng
	public function getTinhByMaVung($maVung){
		$sql = "select * from Tinh where MaVung='$maVung'";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new Tinh($temp['MaTinh'], $temp['TenTinh'], $temp['MaVung']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy danh sách các tỉnh theo mã tin rao vặt
	public function getTinhByMaTRV($maTRV){
		$sql = "select * from tinh where MaTinh in (select MaTinh from chitiettinhraovat where MaTRV='$maTRV')";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new Tinh($temp['MaTinh'], $temp['TenTinh'], $temp['MaVung']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
}











